//
//  MBSpaceAlbumViewController.m
//  MiBaoTu
//
//  Created by admin on 16/9/22.
//  Copyright © 2016年 com.caesartech15. All rights reserved.
//

#import "MBSpaceAlbumViewController.h"
#import "MBSongDetailController.h"
#import "MBLoginController.h"
#import "MBSongDetailForIController.H"
#import "MBSpaceMyParentChildShowPlayViewForIpadController.h"
#import "MBSpaceMyParentChildShowPlayViewController.h"

#import "MBSpaceCollectionMusicCell.h"
#import "SharePLayerView.h"
#import "MBSpaceAlbumHeaderView.h"

#import "MBSongDetailForIpadController.h"

@interface MBSpaceAlbumViewController ()<UITableViewDataSource,UITableViewDelegate,MBSpaceCollectionMusicCellDelegate,SharePLayerViewDelegate,MBSpaceAlbumHeaderViewDelegate>{
    UITableView                         *_tableView;
    MBSpaceAlbumHeaderView              *_tableheaderView;
    CLShareManager                  *shareView;          //分享
    NSMutableArray                      *_mutArr;                 //专辑歌曲列表
    NSMutableDictionary                 *_selectCurrentMusicDic;  //选中cell的歌曲
    NSMutableArray                      *fileIDArr;         //已下载的歌曲列表
    MusicPLayType                       musicPlayType;      //专辑类型
    NSMutableArray                      *collectionMusicArr;
}

@end

@implementation MBSpaceAlbumViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if (!_dateDic) {
        UIImageView*imgView=[[UIImageView alloc]initWithFrame:self.view.bounds];
        imgView.image=[UIImage imageNamed:@"shibai_luntan_5.0"];
        [self.view addSubview:imgView];
        return;
    }
    _selectCurrentMusicDic = [[NSMutableDictionary alloc]init];
    _mutArr = [[NSMutableArray alloc]init];
    fileIDArr = [[NSMutableArray alloc]init];
    collectionMusicArr = [[NSMutableArray alloc]init];
    // Do any additional setup after loading the view.
    [self setTitleView:self.titleString];
    [self setBackItem];
    
    [self setRightPlayerItem];
    
    [self onCreateUI];
    [self onLoadData];
}

#pragma mark - UI
- (void)onCreateUI
{
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.view.height) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.backgroundView = nil;
    [self.view addSubview:_tableView];
    _tableView.tableHeaderView = [self addTableheaderView];
    //分享
    shareView = [[CLShareManager alloc] init];
}

- (MBSpaceAlbumHeaderView *)addTableheaderView
{
    if (!_tableheaderView) {
        _tableheaderView = [[MBSpaceAlbumHeaderView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 275*PERCEN)];
        _tableheaderView.delegate = self;
    }
    [_tableheaderView onUpdateUIForDonwBt:YES];
    _tableheaderView.orientation = self.orientation;
    _tableheaderView.dateDic = _dateDic;
    return _tableheaderView;
}

- (void)onUpdateUIForView:(UIDeviceOrientation)orientation
{
    _tableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, self.view.height);
    _tableView.tableHeaderView = [self addTableheaderView];
    [_tableView reloadData];
}

#pragma mark - 加载数据
- (void)onLoadData
{
    if (!_dateDic) return;
    
    //已下载歌曲记录
    NSArray *downedMusicArr = [UserManager standerDefult].downedMusicArr;
    fileIDArr = [[NSMutableArray alloc]initWithCapacity:downedMusicArr.count];
    for (int i=0; i<downedMusicArr.count; i++) {
        NSDictionary *dic = [downedMusicArr objectAtIndex:i];
        NSString *fileID = [dic objectForKey:@"fileID"];
        [fileIDArr addObject:fileID];
    }
    
    //收藏
    NSArray *musicCollecion = [UserManager standerDefult].collectionAlbumArr;
    [collectionMusicArr removeAllObjects];
    
    if (musicCollecion.count > 0) {
        [collectionMusicArr addObjectsFromArray:musicCollecion];
    }
    
    musicPlayType = MusicPLayType_AlbumList;
    
    //request
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSString *idStr =@"";
    if ([_dateDic.allKeys containsObject:@"ID"]) {
        //专辑音频列表
        idStr = [_dateDic objectForKey:@"ID"];
        [[HTTPManager standerDefault]AFGetRequest:[NSString stringWithFormat:STORY_GET_ALBUMAUDIOLIST_URL_PATH,[NSString stringWithFormat:@"%@",idStr]] view:nil success:^(id json) {
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            
            if ([json[@"ret_code"] integerValue]==0) {
                NSArray *arr = json[@"data"];
                if ([arr isKindOfClass:[NSArray class]]) {
                    [_mutArr addObjectsFromArray:arr];
                    NSString *musicCount = [NSString stringWithFormat:@"%d",(int)_mutArr.count];
                    NSString *musicSize = [NSString stringWithFormat:@"%@",json[@"CapacityTotal"]];
                    _tableheaderView.musicSize = musicSize;
                    _tableheaderView.musicCount = musicCount;
                    
                    BOOL isShowDonw = NO;
                    for (NSDictionary *dic in _mutArr) {
                        NSString *idStr = [NSString stringWithFormat:@"%@",[dic objectForKey:@"AudioID"]];
                        if (![fileIDArr containsObject:idStr]) {
                            isShowDonw = YES;
                        }
                    }
                    [_tableheaderView onUpdateUIForDonwBt:isShowDonw];
                    
                }
            } else { 
                _tableheaderView.musicSize = @"0";
                _tableheaderView.musicCount = @"0";
                [ErrorMessage showErrorMessage:json[@"message"] inView:self.view];
            }
            [_tableView reloadData];
        } failure:^(NSError *error) {
            [MBProgressHUD hideHUDForView:self.view animated:YES];
        }];
    }
    else if ([_dateDic.allKeys containsObject:@"UserID"]) {
        //主播音频
        idStr = [_dateDic objectForKey:@"UserID"];
        [[HTTPManager standerDefault]AFGetRequest:[NSString stringWithFormat:STORY_GET_ANCHORAUDIO_URL_PATH,[NSString stringWithFormat:@"%@",idStr]] view:nil success:^(id json) {
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            
            if ([json[@"ret_code"] integerValue]==0) {
                NSArray *arr = json[@"data"];
                if ([arr isKindOfClass:[NSArray class]]) {
                    [_mutArr addObjectsFromArray:arr];
                    NSString *musicCount = [NSString stringWithFormat:@"%d",(int)_mutArr.count];
                    NSString *musicSize = [NSString stringWithFormat:@"%@",json[@"CapacityTotal"]];
                    _tableheaderView.musicSize = musicSize;
                    _tableheaderView.musicCount = musicCount;
                    
                    BOOL isShowDonw = NO;
                    for (NSDictionary *dic in _mutArr) {
                        NSString *idStr = [NSString stringWithFormat:@"%@",[dic objectForKey:@"AudioID"]];
                        if (![fileIDArr containsObject:idStr]) {
                            isShowDonw = YES;
                        }
                    }
                    [_tableheaderView onUpdateUIForDonwBt:isShowDonw];
                    [_tableView reloadData];
                }
            } else {
                [ErrorMessage showErrorMessage:json[@"message"] inView:self.view];
            }
        } failure:^(NSError *error) {
            [MBProgressHUD hideHUDForView:self.view animated:YES];
        }];
    }
  
    
}

#pragma mark - 表头更新 播放全部 下载全部 更多
- (void)onSelectItemforUpdateUI
{
    CGFloat heights = [_tableheaderView getHeightForNewUI];
    _tableheaderView.frame = CGRectMake(_tableheaderView.frame.origin.x, _tableheaderView.frame.origin.y, _tableheaderView.frame.size.width, 275*PERCEN+heights);
    
    [_tableView beginUpdates];
    [_tableView setTableHeaderView:_tableheaderView];
    [_tableView endUpdates];
}
//播放全部
- (void)onSelectItemforPlayALl
{
    if (_mutArr.count > 0) {
        if ([_dateDic.allKeys containsObject:@"ID"]) {
            [[UserManager standerDefult]playMusicWithMusicArray:_mutArr withCurrentIndex:0 withMusicPlayType:musicPlayType];
        }
        else if ([_dateDic.allKeys containsObject:@"UserID"]) {
            NSString *idStr ;
            UserManager *manager = [UserManager standerDefult];
            //每个主播都有若干个亲子秀，所以需要个判断是哪个主播，这个主播的第几首歌
            if (manager.audioPlayerType != AudioPLayerType_ZhuBo) {
                [[UserManager standerDefult].musicArr removeAllObjects];
                [[UserManager standerDefult]setSelectMusicIndex:0];
                
                //初次进入
                idStr = @"";
                manager.audioPlayerType = AudioPLayerType_ZhuBo;
            } else {
                //2次进入
                //同一个主播，判断是否同一个歌
                NSInteger selectIndex = manager.selectMusicIndex;
                NSArray *arr = manager.musicArr;
                Music *dics = [arr objectAtIndex:selectIndex];
                idStr = [NSString stringWithFormat:@"%@",dics.ID];
                
            }
            [manager setZhubo_SelectIndex:[NSString stringWithFormat:@"%@",@"0"]];
            [manager setSelectMusicIndex:0];
            manager.zhuboDic = (NSMutableDictionary *)_dateDic;
            
            if (ISIPAD) {
                MBSpaceMyParentChildShowPlayViewForIpadController*songDetailVC=[[MBSpaceMyParentChildShowPlayViewForIpadController alloc]init];
                songDetailVC.playTyle = MBSpaceMyParentChildShowPlayType_My;
                
                songDetailVC.dataDic = [UserManager standerDefult].zhuboDic;
                songDetailVC.selectMusicID = [UserManager standerDefult].isLastID;
                [self.navigationController pushViewController:songDetailVC animated:YES];
                return;
            }
            
            
            MBSpaceMyParentChildShowPlayViewController *play = [[MBSpaceMyParentChildShowPlayViewController alloc]init];
            play.playTyle = MBSpaceMyParentChildShowPlayType_My;
            play.dataDic = _dateDic;
            play.selectMusicID = idStr;
            [self.navigationController pushViewController:play animated:YES];
        }
    }
}
//下载全部
- (void)onSelectItemforDownAll
{
    for (int i=0; i<_mutArr.count; i++) {
        NSDictionary *dic = [_mutArr objectAtIndex:i];
         if ([_dateDic.allKeys containsObject:@"ID"]) {
             [[UserManager standerDefult]downLoadMusic:dic withRequestArr:fileIDArr];
         }
        else if ([_dateDic.allKeys containsObject:@"UserID"]) {
            NSDate *date = [NSDate date];
            NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
            [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
            NSString *dateString = [formatter stringFromDate:date];
            NSString *filePath = [dic objectForKey:@"FilePath"];
            NSString *typePath = @"mp3";
            if (![filePath isKindOfClass:[NSNull class]]) {
                typePath = [[filePath componentsSeparatedByString:@"."]lastObject];
                if (filePath.length > 0) {
                    //头像
                    NSString *FaceImg = [dic objectForKey:@"FaceImg"];
                    if ([FaceImg isKindOfClass:[NSNull class]]) {
                        FaceImg = @"";
                    }
                    if (FaceImg.length > 0) {
                        FaceImg = [NSString stringWithFormat:@"%@%@",MUSIC_PLAY_Url,FaceImg];
                    }
                    //背景
                    NSString *CoverPath = [dic objectForKey:@"CoverPath"];
                    if ([CoverPath isKindOfClass:[NSNull class]]) {
                        CoverPath = @"";
                    }
                    if (CoverPath.length > 0) {
                        CoverPath = [NSString stringWithFormat:@"%@%@",MUSIC_PLAY_Url,CoverPath];
                    }
                    [[DownManager staredInstance]downloadWithUrl:[NSString stringWithFormat:@"%@%@",MUSIC_PLAY_Url,filePath]
                                                   withMusicName:[NSString stringWithFormat:@"%@.%@",[dic objectForKey:@"ShowTitle"],typePath]
                                                      withUserID:[NSString stringWithFormat:@"%@",[_dateDic objectForKey:@"UserID"]]
                                                     withMusicID:[NSString stringWithFormat:@"%@",[dic objectForKey:@"ID"]]
                                                     withContent:[NSString stringWithFormat:@"%@",[dic objectForKey:@"Sentiment"]]
                                                   withMusicType:[NSString stringWithFormat:@"%@",@"5"]
                                                      withPicUrl:CoverPath
                                                        withDate:dateString
                                                     withUserPic:FaceImg
                                                    withUserNick:[NSString stringWithFormat:@"%@",[_dateDic objectForKey:@"NickName"]]
                                                        withSize:[NSString stringWithFormat:@"%@",[dic objectForKey:@"Capacity"]]
                                                    withProgress:nil withStats:nil];
            }
        }
    }
    [ErrorMessage showErrorMessage:@"开始下载" inView:self.view];
}
}
//更多
- (void)onSelectItemforMoreAll
{
    BOOL isShowDowned = NO;
    NSString *string = [NSString stringWithFormat:@"%@",[_dateDic objectForKey:@"ID"]];
    if ([_dateDic.allKeys containsObject:@"UserID"]) {
        string = [_dateDic objectForKey:@"UserID"];
    }
    for (NSString *idStr in collectionMusicArr) {
        if ([string intValue] == [idStr intValue]) {
            isShowDowned = YES;
        }
    }
    BOOL isZhuBo = NO;
    if ([_dateDic.allKeys containsObject:@"UserID"] && ![_dateDic.allKeys containsObject:@"AlbumName"]) { //主播
        //只有分享
        isZhuBo = YES;
    }
    
    SharePLayerView *show = [[SharePLayerView alloc]initWithFrame:CGRectZero withType:(self.isFormStroy == NO ? ShowPlayerType_MusicSingleMoew : (isZhuBo==YES?ShowPlayerType_Share:(isShowDowned ? ShowPlayerType_MusicSingleMoew :ShowPlayerType_stroyMore)))];
    show.delegate = self;
    show.orientation = self.orientation;
    [show onShowInWindow];
}

#pragma mark-表头


//跳转到播放器
- (void)onSelectItemForWork
{
    [self onPlayMusic];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 更多操作
- (void)onSelectItemForGouXuanWithValue:(id)value WithSelect:(BOOL)isSelect WithView:(MBSpaceCollectionMusicCell *)view
{
    
}
- (void)onSelectItemForMoewWithValue:(NSDictionary *)value withView:(MBSpaceCollectionMusicCell *)view
{
    
    if (value) {
        [_selectCurrentMusicDic removeAllObjects];
        [_selectCurrentMusicDic addEntriesFromDictionary:value];
        
        BOOL isShowDowned = NO;
        for (NSString *idStr in fileIDArr) {
            NSString *string = [NSString stringWithFormat:@"%@",[_selectCurrentMusicDic objectForKey:@"AudioID"]];
            if ([string isEqualToString:idStr]) {
                isShowDowned = YES;
            }
        }
        BOOL isZhuBo = NO;
        if ([_dateDic.allKeys containsObject:@"UserID"] && ![_dateDic.allKeys containsObject:@"AlbumName"]) { //主播
            //只有分享
            isZhuBo = YES;
        }
        
        SharePLayerView *show = [[SharePLayerView alloc]initWithFrame:CGRectZero withType:(isZhuBo==YES?ShowPlayerType_Share:(isShowDowned == YES ? ShowPlayerType_stroyTabbar :ShowPlayerType_stroyCellMore))];
        show.delegate = self;
        show.orientation = self.orientation;
        [show onShowInWindow];
    }
}

#pragma mark -取消收藏
- (void)onSelctItemForMoreCancelMusicCollection:(SharePLayerView *)view
{
    UserModel *model = [UserManager standerDefult].userModel;
    if (!model.UserID) {
        MBLoginController *registerLastVC = [[MBLoginController alloc] init];
        registerLastVC.formBaseViewCtrl = NSStringFromClass([self class]);
        [self.navigationController pushViewController:registerLastVC animated:YES];
        return;
    }

    
    //取消收藏
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSString *idStr = [_dateDic objectForKey:@"ID"];//专辑
    if ([_dateDic.allKeys containsObject:@"UserID"] && ![_dateDic.allKeys containsObject:@"AlbumName"]) { //主播
        idStr = [_dateDic objectForKey:@"UserID"];
    }
    [[HTTPManager standerDefault]AFGetRequest:[NSString stringWithFormat:SPACE_COLLECTION_REMOVE_URL_PATH,model.UserID,idStr,@2] view:nil success:^(id json) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        
        if ([json[@"ret_code"] integerValue]==0) {
            
            //数据更新
            [collectionMusicArr removeObject:idStr];
            [[UserManager standerDefult].collectionAlbumArr removeAllObjects];
            [[UserManager standerDefult].collectionAlbumArr addObjectsFromArray:collectionMusicArr];
            
        }
        [ErrorMessage showErrorMessage:json[@"message"] inView:self.view];
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];

}

#pragma mark -收藏
- (void)onSelctItemForMoreCollectionMusic:(SharePLayerView *)view
{
    UserModel *model = [UserManager standerDefult].userModel;
    if (!model.UserID) {
        MBLoginController *registerLastVC = [[MBLoginController alloc] init];
        registerLastVC.formBaseViewCtrl = NSStringFromClass([self class]);
        [self.navigationController pushViewController:registerLastVC animated:YES];
        return;
    }

    
    //收藏
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSString *idStr = [_dateDic objectForKey:@"ID"];//专辑
    if ([_dateDic.allKeys containsObject:@"UserID"] && ![_dateDic.allKeys containsObject:@"AlbumName"]) { //主播
        idStr = [_dateDic objectForKey:@"UserID"];
    }
    [[HTTPManager standerDefault]AFGetRequest:[NSString stringWithFormat:SPACE_COLLECTION_COLLECION_URL_PATH,model.UserID,idStr,@2] view:nil success:^(id json) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        
        if ([json[@"ret_code"] integerValue]==0) {
            
            //数据更新
            [collectionMusicArr addObject:idStr];
            [[UserManager standerDefult].collectionAlbumArr removeAllObjects];
            [[UserManager standerDefult].collectionAlbumArr addObjectsFromArray:collectionMusicArr];
            
            [self onSelectItemforDownAll];
        } 
        [ErrorMessage showErrorMessage:json[@"message"] inView:self.view];
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}

#pragma mark - 播放、分享、取消收藏、下载
//播放
- (void)onSelctItemForMorePlayMusic:(SharePLayerView *)view
{

    if ([_dateDic.allKeys containsObject:@"ID"]) {
        if (_selectCurrentMusicDic.allKeys.count == 0) {
            //第一次
            [[UserManager standerDefult]playMusicWithMusicArray:_mutArr withCurrentIndex:0 withMusicPlayType:musicPlayType];
        } else if ([_mutArr containsObject:_selectCurrentMusicDic]) {
            int index = (int)[_mutArr indexOfObject:_selectCurrentMusicDic];
            //当前页面播放
            [[UserManager standerDefult]playMusicWithMusicArray:_mutArr withCurrentIndex:index withMusicPlayType:musicPlayType];
        }
    }
    else if ([_dateDic.allKeys containsObject:@"UserID"]) {
        NSString *idStr ;
        UserManager *manager = [UserManager standerDefult];
        //每个主播都有若干个亲子秀，所以需要个判断是哪个主播，这个主播的第几首歌
        if (manager.audioPlayerType != AudioPLayerType_ZhuBo) {
            [[UserManager standerDefult].musicArr removeAllObjects];
            [[UserManager standerDefult]setSelectMusicIndex:0];
            
            //初次进入
            idStr = @"";
            manager.audioPlayerType = AudioPLayerType_ZhuBo;
        } else {
            //2次进入
            //同一个主播，判断是否同一个歌
            NSInteger selectIndex = manager.selectMusicIndex;
            NSArray *arr = manager.musicArr;
            Music *dics = [arr objectAtIndex:selectIndex];
            idStr = [NSString stringWithFormat:@"%@",dics.ID];
            
        }
        [manager setZhubo_SelectIndex:[NSString stringWithFormat:@"%@",@"0"]];
        [manager setSelectMusicIndex:0];
        manager.zhuboDic = (NSMutableDictionary *)_dateDic;
        
        if (ISIPAD) {
            MBSpaceMyParentChildShowPlayViewForIpadController*songDetailVC=[[MBSpaceMyParentChildShowPlayViewForIpadController alloc]init];
            songDetailVC.playTyle = MBSpaceMyParentChildShowPlayType_My;
            
            songDetailVC.dataDic = [UserManager standerDefult].zhuboDic;
            songDetailVC.selectMusicID = [UserManager standerDefult].isLastID;
            [self.navigationController pushViewController:songDetailVC animated:YES];
            return;
        }
        
        
        MBSpaceMyParentChildShowPlayViewController *play = [[MBSpaceMyParentChildShowPlayViewController alloc]init];
        play.playTyle = MBSpaceMyParentChildShowPlayType_My;
        play.dataDic = _dateDic;
        play.selectMusicID = idStr;
        [self.navigationController pushViewController:play animated:YES];
    }
}
//分享
- (void)onSelctItemForMoreShareMusic:(SharePLayerView *)view
{
    
    NSString *audioName = [_dateDic objectForKey:@"AlbumName"];
    audioName = [[audioName componentsSeparatedByString:@"."]firstObject];
    NSString *url = [NSString stringWithFormat:SHARE_MUSIC_URL_PATH,[_dateDic objectForKey:@"ID"]];
    if (view.type == ShowPlayerType_MusicSingleMoew || view.type == ShowPlayerType_stroyMore) { //分享和收藏 、分享和取消收藏
        //专辑
        url = [NSString stringWithFormat:SHARE_ALBUM_URL_PATH,[_dateDic objectForKey:@"ID"]];
        [shareView addAudioId:[NSString stringWithFormat:@"%@",[_dateDic objectForKey:@"ID"]] withType:@2];
    }
    else if (view.type == ShowPlayerType_Share) { //亲子秀
        //分享
        audioName = [_dateDic objectForKey:@"NickName"];
        NSDictionary *dic = [_mutArr objectAtIndex:0];
        url = [NSString stringWithFormat:SHARE_SHOW_URL_PATH,[dic objectForKey:@"ID"]];
        [shareView addAudioId:[NSString stringWithFormat:@"%@",[dic objectForKey:@"ID"]] withType:@3];
    }
    else { //音频
       url = [NSString stringWithFormat:SHARE_MUSIC_URL_PATH,[_dateDic objectForKey:@"ID"]];
        [shareView addAudioId:[NSString stringWithFormat:@"%@",[_dateDic objectForKey:@"ID"]] withType:@1];
    }
    [shareView setShareVC:self content:[NSString stringWithFormat:@"分享:%@",audioName] image:[UIImage imageNamed:@"logo_dl_29"] url:url];
    [shareView show];
}

//下载
- (void)onSelctItemForMoreDownMusic:(SharePLayerView *)view
{
    if (view.type == ShowPlayerType_stroyCellMore ) {
        if ([_dateDic.allKeys containsObject:@"ID"]) {
            [[UserManager standerDefult]downLoadMusic:_selectCurrentMusicDic withRequestArr:fileIDArr];
        }
        else if ([_dateDic.allKeys containsObject:@"UserID"]) {
            NSDate *date = [NSDate date];
            NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
            [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
            NSString *dateString = [formatter stringFromDate:date];
            NSString *filePath = [_selectCurrentMusicDic objectForKey:@"FilePath"];
            NSString *typePath = @"mp3";
            if (![filePath isKindOfClass:[NSNull class]]) {
                typePath = [[filePath componentsSeparatedByString:@"."]lastObject];
                if (filePath.length > 0) {
                    //头像
                    NSString *FaceImg = [_selectCurrentMusicDic objectForKey:@"FaceImg"];
                    if ([FaceImg isKindOfClass:[NSNull class]]) {
                        FaceImg = @"";
                    }
                    if (FaceImg.length > 0) {
                        FaceImg = [NSString stringWithFormat:@"%@%@",MUSIC_PLAY_Url,FaceImg];
                    }
                    //背景
                    NSString *CoverPath = [_selectCurrentMusicDic objectForKey:@"CoverPath"];
                    if ([CoverPath isKindOfClass:[NSNull class]]) {
                        CoverPath = @"";
                    }
                    if (CoverPath.length > 0) {
                        CoverPath = [NSString stringWithFormat:@"%@%@",MUSIC_PLAY_Url,CoverPath];
                    }
                    [[DownManager staredInstance]downloadWithUrl:[NSString stringWithFormat:@"%@%@",MUSIC_PLAY_Url,filePath]
                                                   withMusicName:[NSString stringWithFormat:@"%@.%@",[_selectCurrentMusicDic objectForKey:@"ShowTitle"],typePath]
                                                      withUserID:[NSString stringWithFormat:@"%@",[_dateDic objectForKey:@"UserID"]]
                                                     withMusicID:[NSString stringWithFormat:@"%@",[_selectCurrentMusicDic objectForKey:@"ID"]]
                                                     withContent:[NSString stringWithFormat:@"%@",[_selectCurrentMusicDic objectForKey:@"Sentiment"]]
                                                   withMusicType:[NSString stringWithFormat:@"%@",@"5"]
                                                      withPicUrl:CoverPath
                                                        withDate:dateString
                                                     withUserPic:FaceImg
                                                    withUserNick:[NSString stringWithFormat:@"%@",[_dateDic objectForKey:@"NickName"]]
                                                        withSize:[NSString stringWithFormat:@"%@",[_selectCurrentMusicDic objectForKey:@"Capacity"]]
                                                    withProgress:nil withStats:nil];
                }
            }
        }
        [ErrorMessage showErrorMessage:@"开始下载" inView:self.view];
    }
}

#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _mutArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //歌曲
    static NSString *cellID = @"Collection_Music_Cell";
    MBSpaceCollectionMusicCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[MBSpaceCollectionMusicCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.delegate = self;
    }
    //是否隐藏更多按钮
    cell.isHideMoewBt = NO;
    [cell onCompileCellForLayout:NO WithAllSelct:NO];
    cell.orientation = self.orientation;

    cell.isShowPlayCount = YES;
    if (_mutArr.count > indexPath.row && indexPath.row >= 0) {
        NSDictionary *dic = [_mutArr objectAtIndex:indexPath.row];
        cell.musicDic = dic;
        
        BOOL isShowDowned = NO;
        for (NSString *idStr in fileIDArr) {
            NSString *string = [NSString stringWithFormat:@"%@",[dic objectForKey:@"AudioID"]];
            if ([string isEqualToString:idStr]) {
                isShowDowned = YES;
            }
        }
        cell.isShowDowned = isShowDowned;
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100*PERCEN;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *dic = [_mutArr objectAtIndex:indexPath.row];
    if ([[dic objectForKey:@"AudioStatus"] intValue] == 0) {
//        [ErrorMessage showErrorMessage:@"" inView:self.view];
//        return;
    }
    
    if (_type == MBStorySceneCellType_HotAnchor) {
#pragma mark - 人气主播播放器 标准   
    
        
        NSString *idStr ;
        UserManager *manager = [UserManager standerDefult];
        //每个主播都有若干个亲子秀，所以需要个判断是哪个主播，这个主播的第几首歌
        if (manager.audioPlayerType != AudioPLayerType_ZhuBo) {
            [[UserManager standerDefult].musicArr removeAllObjects];
            [[UserManager standerDefult]setSelectMusicIndex:0];
            
            //初次进入
            idStr = @"";
            manager.audioPlayerType = AudioPLayerType_ZhuBo;
        } else {
            //2次进入
            //同一个主播，判断是否同一个歌
            NSInteger selectIndex = manager.selectMusicIndex;
            NSArray *arr = manager.musicArr;
            Music *dics = [arr objectAtIndex:selectIndex];
            idStr = [NSString stringWithFormat:@"%@",dics.ID];
            
        }
        [manager setZhubo_SelectIndex:[NSString stringWithFormat:@"%@",@"0"]];
        [manager setSelectMusicIndex:indexPath.row];
        manager.zhuboDic = (NSMutableDictionary *)_dateDic;
        
        if (ISIPAD) {
            MBSpaceMyParentChildShowPlayViewForIpadController*songDetailVC=[[MBSpaceMyParentChildShowPlayViewForIpadController alloc]init];
            songDetailVC.playTyle = MBSpaceMyParentChildShowPlayType_My;
            
            songDetailVC.dataDic = _dateDic;
            songDetailVC.selectMusicID = idStr;
            [self.navigationController pushViewController:songDetailVC animated:YES];
            return;
        }
        
        
        MBSpaceMyParentChildShowPlayViewController *play = [[MBSpaceMyParentChildShowPlayViewController alloc]init];
        play.playTyle = MBSpaceMyParentChildShowPlayType_My;
        play.dataDic = _dateDic;
        play.selectMusicID = idStr;
        [self.navigationController pushViewController:play animated:YES];
    } else {
#pragma mark - 音频播放器 标准
        
        if (_selectCurrentMusicDic.allKeys.count == 0) {
            //第一次
            [[UserManager standerDefult]playMusicWithMusicArray:_mutArr withCurrentIndex:indexPath.row withMusicPlayType:musicPlayType];
        } else if ([UserManager standerDefult].selectMusicIndex != indexPath.row) {
            //当前页面播放
            [[UserManager standerDefult]playMusicWithMusicArray:_mutArr withCurrentIndex:indexPath.row withMusicPlayType:musicPlayType];
        } else {
            //2次点击进入播放页面
            if ([UserManager standerDefult].musicArr.count > 0) {
                //歌曲列表没歌曲 无需进入
                UserManager *manager = [UserManager standerDefult];
                [manager setSelectMusicIndex:indexPath.row];
                if (manager.audioPlayerType != AudioPLayerType_YinPin) {
                    [manager setIsPlaying:NO];
                    [manager setAudioPlayerType:AudioPLayerType_YinPin];
                } else {
                    [manager setIsPlaying:YES];
                }
                //            if (ISIPAD) {
                //                MBSongDetailForIController*song=[[MBSongDetailForIController alloc]init];
                //                [self.navigationController pushViewController:song animated:YES];
                //            }
                if (ISIPAD) {
                    MBSongDetailForIpadController*song=[[MBSongDetailForIpadController alloc]init];
                    [self.navigationController pushViewController:song animated:YES];
                    return;
                }
                MBSongDetailController *song = [[MBSongDetailController alloc]init];
                [self.navigationController pushViewController:song animated:YES];
            }
        }
    }
    [_selectCurrentMusicDic removeAllObjects];
    [_selectCurrentMusicDic addEntriesFromDictionary:dic];
    

}

@end
